<?

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

if (empty($_POST['id'])) {
  header('Location: dashboard.php');
}

include_once 'controller.inc.php';

$id = $_POST['id'];
$failed = FALSE;

$db_link = db_connect();

$user = db_getUser($id);
if ($user->getEmail() == $_COOKIE['user']) {
  db_deleteUser($user);
  
  $roles = db_getRolesForUser($user);
  foreach ($roles as $role) {
    $role->setUser(NULL);
  }
}
else {
  $failed = TRUE;
}

db_disconnect($db_link);

if ($failed) {
  header('Location: dashboard.php?failure=auth');
}
else {
  setcookie('user', '', time() - 3600);
  header('Location: index.php?success=Account_deleted');
}

?>
